﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
using INSTA.Library;

namespace INSTA.BankApplication.DAL
{
    /// <summary>
    /// Represents all informations about a property
    /// that represents a database field.
    /// </summary>
    public class MappedProperty
    {

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="property">The property.</param>
        public MappedProperty(PropertyInfo property, DataMapper mapper)
        {
            // Set the mapped object.
            MappedObject = mapper;

            // set the property name from the property info.
            PropertyName = property.Name;

            // Le'ts get the FieldAttribute that decorates the property.
            var fieldAttribute  = property.GetUniqueAttribute<FieldAttribute>();

            // Fill the infos from the attribute.
            FieldName           = fieldAttribute.FieldName;
            IsPrimaryKey        = fieldAttribute.IsPrimaryKey;

            
        }


        /**********************************************************************
         * 
         * Properties
         * 
         **********************************************************************/

        /// <summary>
        /// Gets or sets the mapped object.
        /// </summary>
        /// <value>The mapped object.</value>
        public DataMapper MappedObject { get; set; }


        /// <summary>
        /// Gets or sets the name of the property.
        /// </summary>
        public string PropertyName { get; set; }

        /// <summary>
        /// Gets or sets the name of the database field.
        /// </summary>
        /// <value>The name of the field.</value>
        public string FieldName { get; set; }

        /// <summary>
        /// Indicates if the mapped property represents 
        /// the mapped table's primary key.
        /// </summary>
        /// <value>
        /// true if the mapped property represents the 
        /// primary key, else false.
        /// </value>
        public bool IsPrimaryKey { get; set; }

        /// <summary>
        /// Gets or sets the property value.
        /// </summary>
        public object PropertyValue
        {
            get
            {
                return MappedObject.GetPropertyValue(PropertyName);
            }

            set
            {
                MappedObject.SetPropertyValue(PropertyName, value);
            }
        }

    }
}
